package com.demo.javaee;

import java.util.HashMap;

/**
 * @author shenguangyang
 */
public class HashMapExample {
    public static void main(String[] args) {
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("test", 1);
        System.out.println(hashMap.get("test"));
        testHash();
    }

    /**
     * 有人怀疑两种运算效率差别到底有多少，我做个测试：
     * <p>
     * 直接【求余】和【按位】运算的差别验证
     */
    public static void testHash() {
        long currentTimeMillis = System.currentTimeMillis();
        int a = 0;
        int times = 10000 * 100000;
        for (long i = 0; i < times; i++) {
            a = 9999 % 1024;
        }
        long currentTimeMillis2 = System.currentTimeMillis();

        int b = 0;
        for (long i = 0; i < times; i++) {
            b = 9999 & (1024 - 1);
        }

        long currentTimeMillis3 = System.currentTimeMillis();
        System.out.println(a + "," + b);
        System.out.println("%: " + (currentTimeMillis2 - currentTimeMillis));
        System.out.println("&: " + (currentTimeMillis3 - currentTimeMillis2));
    }
}
